package com.dd.service;

import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.net.URLDecoder;

import javax.servlet.http.HttpServletRequest;

import com.dd.dao.DdUserDAO;
import com.dd.entity.*;

public class UserService {
	private DdUserDAO ddUserDAO;

	public DdUserDAO getDdUserDAO() {
		return ddUserDAO;
	}

	public void setDdUserDAO(DdUserDAO ddUserDAO) {
		this.ddUserDAO = ddUserDAO;
	}

	public String toChi(String input) {
		try {
			byte[] bytes = input.getBytes("utf-8");
			return new String(bytes, "utf-8");
		} catch (Exception ex) {
		}
		return null;
	}

	public void userInfo(HttpServletRequest request, DdUser du, StringBuffer sbf) {

		float ft = du.getAmount() + du.getAmountfree();
		int scale = 2;// 设置位数
		int roundingMode = 4;// 表示四舍五入，可以选择其他舍值方式，例如去尾，等等.
		BigDecimal bd = new BigDecimal((double) ft);
		bd = bd.setScale(scale, roundingMode);
		ft = bd.floatValue();
		
		float ft2 = du.getAmount();
		
		bd = new BigDecimal((double) ft2);
		bd = bd.setScale(scale, roundingMode);
		ft2 = bd.floatValue();
		
		sbf.append("<user logid=\""+du.getLogid()+"\" id=\"" + du.getId() + "\" name=\""
				+ (du.getName() == null ? "" : du.getName()) + "\" sex=\""
				+ (du.getSex() == null ? "" : du.getSex()) + "\" phone=\""
				+ du.getPhone() + "\" email=\""
				+ (du.getEmail() == null ? "" : du.getEmail()) + "\" age=\""
				+ (du.getAge() == null ? "" : du.getAge())
				+ "\" occupation =\""
				+ (du.getOccupation() == null ? "" : du.getOccupation())
				+ "\" company=\""
				+ (du.getCompany() == null ? "" : du.getCompany())
				+ "\" nickname=\""
				+ (du.getNickname() == null ? "" : du.getNickname())
				+ "\" address=\""
				+ (du.getAddress() == null ? "" : du.getAddress())
				+ "\" aera=\"" + (du.getArea() == null ? "" : du.getArea())
				+ "\" road=\"" + (du.getRoad() == null ? "" : du.getRoad())
				+ "\" address2=\""
				+ (du.getAddress2() == null ? "" : du.getAddress2())
				+ "\" area2=\"" + (du.getArea2() == null ? "" : du.getArea2())
				+ "\" road2=\"" + (du.getRoad2() == null ? "" : du.getRoad2())
				+ "\" income=\""
				+ (du.getIncome() == null ? "" : du.getIncome())
				+ "\" credit=\""
				+ (du.getCredit() + "\" regdate=\"" + du.getRegdate())
				+ "\" money=\""+ft+"\" amount=\""+ft2+"\" score=\""+du.getScore()+"\" />");

	}

	public void editUser(HttpServletRequest request, DdUser du, StringBuffer sbf)
			throws UnsupportedEncodingException {
		
		if (request.getParameter("phone") != null) {
			du.setPhone(request.getParameter("phone"));
		}
		if (request.getParameter("nickname") != null) {
			du.setNickname(request.getParameter("nickname"));
		}
		if (request.getParameter("name") != null) {
			du.setName(request.getParameter("name"));
		}
		if (request.getParameter("sex") != null) {
			du.setSex(Integer.parseInt(request.getParameter("sex")));
		}
		if (request.getParameter("email") != null) {
			du.setEmail(request.getParameter("email"));
		}
		if (request.getParameter("age") != null) {
			du.setAge(Integer.parseInt(request.getParameter("age")));
		}
		if (request.getParameter("occuption") != null) {
			du.setOccupation(request.getParameter("occuption"));
		}
		if (request.getParameter("company") != null) {
			du.setCompany(request.getParameter("company"));
		}
		if (request.getParameter("address") != null) {
			du.setAddress(request.getParameter("address"));
		}
		if (request.getParameter("area") != null) {
			du.setArea(request.getParameter("area"));
		}
		if (request.getParameter("road") != null) {
			du.setRoad(request.getParameter("road"));
		}
		if (request.getParameter("address2") != null) {
			du.setAddress2(request.getParameter("address2"));
		}
		if (request.getParameter("area2") != null) {
			du.setArea2(request.getParameter("area2"));
		}
		if (request.getParameter("road2") != null) {
			du.setRoad2(request.getParameter("road2"));
		}
		if (request.getParameter("income") != null) {
			du.setIncome(request.getParameter("income"));
		}
		if (request.getParameter("newpassword") != null) {
			String oldpassword = request.getParameter("oldpassword");
			if (oldpassword == null || !du.getPassword().equals(oldpassword)) {
				sbf.append("<result>2</result></root>");
				return;
			}
			du.setPassword(request.getParameter("newpassword"));
		}
		this.getDdUserDAO().saveOrUpdate(du);
		sbf.append("<result>1</result></root>");
	}

}
